package com.wxsong.authplatform.authweb.service.menu;

import com.google.common.collect.Maps;
import com.wxsong.authplatform.authweb.dao.authority.AuthorityDao;
import com.wxsong.authplatform.authweb.dao.menu.MenuDao;
import com.wxsong.authplatform.authweb.dao.role.RoleDao;
import com.wxsong.authplatform.authweb.dao.users.UserDao;
import com.wxsong.authplatform.authweb.model.menu.Menu;
import com.wxsong.authplatform.authweb.model.users.Users;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
@Transactional(rollbackFor = Exception.class)
public class MenuServiceImpl implements MenuService {

    @Resource
    private MenuDao menuDao;
    @Resource
    private UserDao userDao;
    @Resource
    private RoleDao roleDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS, readOnly = true)
    public Menu getByName(String name) {
        return menuDao.getByName(name);
    }

    @Override
    public List<Menu> list(int from, int pageSize, Map<String, Object> paramMap) {
        return menuDao.list(from, pageSize, paramMap);
    }

    @Override
    public Long count(Map<String, Object> paramMap) {
        return menuDao.count(paramMap);
    }

    @Override
    public int add(Menu menu) {
        return menuDao.add(menu);
    }

    @Override
    public int updateInfo(Long id, String name,String url, String desc){
        Map paramMap = Maps.newHashMap();
        paramMap.put("id", id);
        paramMap.put("name", name);
        paramMap.put("url", url);
        paramMap.put("description", desc);
        return menuDao.update(paramMap);
    }

    @Override
    public int delete(Long[] ids) {
        return menuDao.delete(ids);
    }

    @Override
    public Menu getById(Long id) {
        return menuDao.getById(id);
    }

    @Override
    public List<Menu> getByParentId(Long parentId) {
        return menuDao.getByParentId(parentId);
    }

    @Override
    public List<Menu> getByParentCode(String parentCode, long curUserId) {
        return menuDao.getByParentCode(parentCode, curUserId);
    }

    @Override
    public List<Menu> findOwnFirstMenu(long userId) {
        Users users = userDao.getById(userId);
//        String roles = users.getRoles();
//        String[] idArray = roles.split(",");
//        Long[] ids = new Long[idArray.length];
//        for (int i=0; i<idArray.length; i++){
//            ids[i] = Long.valueOf(idArray[i]);
//        }
//        List<>roleDao.getByIds(ids);
//        return menuDao.findOwnFirstMenuByRoles(ids);
        return null;
    }



}
